﻿namespace USElections.Converters
{
    using System;
    using System.Windows.Data;
    using System.Windows.Media;
    using USElections.Model;

    public class PartyToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            Candidate.Parties party = Candidate.Parties.None;

            try
            {
                string input = value.ToString();

                if (input.EndsWith(" Party"))
                {
                    input = input.Substring(0, input.Length - 6);
                }
                
                party = (Candidate.Parties)Enum.Parse(typeof(Candidate.Parties), input, true);
            }
            catch
            {
            }

            if (party == Candidate.Parties.Democratic)
            {
                return new SolidColorBrush(Colors.Blue);
            }
            else if (party == Candidate.Parties.Republican)
            {
                return new SolidColorBrush(Colors.Red);
            }
            else if (party == Candidate.Parties.Green)
            {
                return new SolidColorBrush(Colors.Green);
            }
            else if (party == Candidate.Parties.Undecided)
            {
                return new SolidColorBrush(Colors.Black);
            }

            return new SolidColorBrush(Colors.Gray);
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}
